﻿#include <stdio.h>
#include <fstream>
#include <map>
#include "Aho–Corasick.h"
#include <string>
#include <iostream>
using namespace std;

void readWords(string path, vector<string>& words) {
	ifstream file(path.c_str());
	if (file.is_open()) {
		string line;
		while (file.good()) {
			getline(file, line);
			if (line.length()>0) {
				words.push_back(line);
			}
		}
		file.close();
	}
}

void processText(string inputFile, string outputFile, AhoCorasick& ak, map<int, vector<int>>& result) {

        ifstream inFile(inputFile.c_str());
        ofstream outFile(outputFile.c_str());
        if ((inFile.is_open()) && (outFile.is_open()))
        {
			string inLine;
			while (inFile.good())
			{
				getline(inFile,inLine);
				string outLine;
				ak.search(result, inLine, outLine); 
				for (int i = 0; i<outLine.length(); ++i ) {
					if (outLine[i] != '*') {
						outFile<<outLine[i];
					} 
				}
				outFile<<endl;
			}
			inFile.close();
			outFile.close();
		} else {
			cout<<"Error file"<<endl;
			system("exit");
		}
}


int main() {

        string newText;
        vector<string> words;
        readWords("stopwords.txt", words);

        AhoCorasick ak(words);
        ak.buildAutomat(); 
        map<int, vector<int>> result;
        processText("input.txt", "output.txt", ak, result);
        
        for (map<int, vector<int>>::iterator itr=result.begin(); itr!=result.end(); itr++) {
			cout<<words[itr->first]<<":"<<endl;
			for (vector<int>::iterator itr2=itr->second.begin(); itr2!=itr->second.end(); itr2++) {
				cout<<(*itr2)<<" ";
			}
			cout<<endl;
        }
		system("pause");
        return 0;
}